Because it’s fun
Because it’s cool
Because it allows a new dimension of conveying results
Because a collaborator asked
plotly - framework for interactive plotting (covered a lot)
ggiraph - R package that creates interactive ggplot graphs using D3 and other frameworks
highcharter - wraps JS framework Highcharts
rCharts - interactive javascript visualizations from R using a familiar lattice style plotting interface
:::
rbokeh - another framework for interactive plotting
leaflet - great for interactive maps
rayshader - interactive 3D maps/plots/ggplot2s
d3heatmap - interactive heatmamps
threejs - 3D scatterplots
:::
Many cases you know how to build a ggplot2 object
May want to “interactive” it up
Many times you will hit roadblocks or limitations
We’re going to be using the toyest data, mtcars ! But we want the car names
(cars <- as_tibble(mtcars) %>%
rownames_to_column(var = "car") %>%
separate(car, into = c("make", "blah"), sep = " ", remove = FALSE, extra = "merge") %>%
select(make, car, everything()) %>%
select(-blah))# A tibble: 32 × 13
make car mpg cyl disp hp drat wt qsec vs am gear carb
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
2 2 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
3 3 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
4 4 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
5 5 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
6 6 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
7 7 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
8 8 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
9 9 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
10 10 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
# ℹ 22 more rows
Weight (in tons) vs miles per gallon (MPG)
plotly::ggplotly turns gg objects to plotly objects!
Does a lot of the geoms
https://plotly.com/r/plotly-fundamentals/ is a great resource for starting (maybe 2 hours?)
Uses pipes not +. Main function is plot_ly (guesses type). Treats variable types as continuous if numbers.
No trace type specified:
Based on info supplied, a 'scatter' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#scatter
No scatter mode specifed:
Setting the mode to markers
Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
And can do factor on the fly like normal formulas.
+ (but in quotes)Can do very basic things.
Lots of different options. Has pros/cons vs. ggplot2.
(miniplot <- plotly::plot_ly() %>%
add_trace(x = c(1, 2, 3), y = c(4, 3, 2), mode='lines') %>%
add_trace(x = c(20, 30, 40), y = c(30, 40, 50),
xaxis='x2', yaxis='y2', mode='lines') %>% # new axis
layout(xaxis2 = list(domain = c(0.6, 0.95), anchor='y2'), # anchor based on axis
yaxis2 = list(domain = c(0.6, 0.95), anchor='x2')))In ggiraph you cannot transform a ggplot object but you have the same syntax, but put an _interactive on it.
girafe it?Highcharts offers both a commercial license as well as a free non-commercial license
n1 <- car_count %>%
nPlot(n ~ producer, group = "cylinders", data = ., type = "multiBarChart")
n1$show('inline', include_assets = TRUE, cdn = TRUE)n2 <- car_count %>%
nPlot(pct ~ producer, group = "cylinders", data = ., type = "multiBarChart")
n2$show('inline', include_assets = TRUE, cdn = TRUE)Show what you’re trying to show, stop making it too much
Hone in on the insights you found (don’t be too subtle)
You should be able to get the gist before you read anything
Make Labels Larger
Label Panels/Facets/Subplots (A, B, C, D)
Keep colors consistent if connected and distinct if not
Watch out for yellow with white text or on white background (projector)
Caption your figures
Interactive graphics are sometimes powerful, but limited to HTML
Many different frameworks, customization ease is highly variable
Remember basic plotting tenets: show what you want to show, hone in on the insights (don’t be sutle
plotly good general framework, has a ggplot2 wrapper, but highcharts has some built in niceties.
crosstalksd <- SharedData$new(quakes[sample(nrow(quakes), 100),])
# Use SharedData like a dataframe with Crosstalk-enabled widgets
bscols(
leaflet(sd) %>% addTiles() %>% addMarkers(),
datatable(sd, extensions="Scroller", style="bootstrap", class="compact", width="100%",
options=list(deferRender=TRUE, scrollY=300, scroller=TRUE))
)Much easier to do within the same framework (e.g. Plotly)
Possible do it, but need to know JavaScript
Can use Shiny (see https://mastering-shiny.org/action-graphics.html)
Can use crosstalk (https://rstudio.github.io/crosstalk/)
Must be a subset of certain htmlwidgets
DT - data tables
Leaflet
Plotly
The purpose of visualization is insight, not pictures. ―Ben Shneiderman
Above all else, show the data. ―Edward Tufte